<?php defined('SYSPATH') or die('No direct script access.');

header("Content-Type:text/html;charset=utf-8");

class Controller_Admin_Common extends Controller {
    
    public function action_uploadFile(){

        $m=$this->request->post("model");

        $name=$this->request->post("name");

        $file=$_FILES[$name];

        $filepath=$file['tmp_name'];

        $filename=$file['name'];

        if (preg_match("/[\x7f-\xff]/", $filename)){

            $filename="temp".date('Ymd').rand(1,9999);

        }

        else{

            $filename=date('Ymd').rand(1,9999).$filename;
        }

        $id=$this->request->post("id");

        if($m!=''){

            $model=Model::factory("Admin_".$m);

            $result=$model->uploadImage($id,$filepath,$filename); 

        }
        else{
            
            $upFilePath ='temp/t/'.$filename;

            $model=new Model_Admin_Common();

            $result=$model.upload_file($filepath,$upFilePath,'');

        }

        if($result){

            echo json_encode(array("info"=>"success"));
        }
        else{

            echo json_encode(array("info"=>"对不起，上传失败"));
        }
    }
    public function action_make_img(){

        $spu=$this->request->post("spu");

        $img_name=$this->request->post("img");

        try {

            $img = Image::factory($img_name);

            $filename ='temp/images/product/'.$spu.'-M-400400.jpg';

            $img->resize(400, 400)->save($filename);

            $filename ='temp/images/product/'.$spu.'-M-260260.jpg';

            $img->resize(260, 260)->save($filename);

            $filename ='temp/images/product/'.$spu.'-M-100100.jpg';

            $img->resize(100, 100)->save($filename);

            $model=new Model_Admin_Product();

            $sku_info=$model->getSkuInfo($spu);

            if(!empty($sku_info)){
                foreach ($sku_info as $key => $sku) {
                    if(!empty($sku['image'])){

                         $img = Image::factory($sku['image']);

                        $filename ='temp/images/product/'.$sku['sku'].'-M-400400.jpg';

                        $img->resize(400, 400)->save($filename);

                        $filename ='temp/images/product/'.$sku['sku'].'-M-260260.jpg';

                        $img->resize(260, 260)->save($filename);

                        $filename ='temp/images/product/'.$sku['sku'].'-M-100100.jpg';

                        $img->resize(100, 100)->save($filename);   
                    }
                }
            }
            echo json_encode(array("info"=>"success"));
        } 
        catch (Exception $e) {
            echo json_encode(array("info"=>"faild"));
        }
    } 
}
?>